package com.rongc.client.freight.business.mine.view.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.LogUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;
import com.rongc.client.freight.business.mine.request.api.ProblemApi;
import com.rongc.client.freight.business.mine.view.adapter.RecyclerProblemAdapter;
import com.rongc.client.freight.utils.UtilMethod;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * Created by ccqx on 2017/7/20.
 */

public class ProblemActivity extends BaseActivity {
    @Bind(R.id.recycler)
    RecyclerView mRecyclerView;
    RecyclerProblemAdapter modelAdapter;

    List<JSONObject> datas;

    Response.Listener<JSONArray> respOrderListener = new Response.Listener<JSONArray>() {

        @Override
        public void onResponse(JSONArray response) {
            if (HandleCode.requestSuccess()) {
                datas.clear();
                if(response != null)
                for(int i =0;i<response.length();i++){

                    datas.add(response.optJSONObject(i));

                }
                modelAdapter.notifyDataSetChanged();
            } else {
                UtilMethod.dismissProgressDialog(ProblemActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            LogUtils.i("error.getMessage(): " + error.getMessage());
            UtilMethod.dismissProgressDialog(ProblemActivity.this);
        }
    };

    @Override
    public void initData() {
        RequestManager.getInstance().call(new ProblemApi(new ProblemApi.ProblemParams(),
                respOrderListener, errorListener));
    }

    @Override
    public void initView() {
        initToolbar(R.string.nor_app_question);
        datas = new ArrayList<>();
        modelAdapter = new RecyclerProblemAdapter(this,datas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(modelAdapter);
    }

    @Override
    public int getLayoutId() {
        return R.layout.universal_recycler;
    }
}
